package com.ericsson.enap.modules.ebmLog.task.vgc;

import com.ericsson.common.quartz.task.ManualTask;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.common.util.RedisCache;
import com.ericsson.enap.modules.ebmLog.entity.vgc.VgcNetEntity;
import com.ericsson.enap.modules.ebmLog.service.impl.vgc.VgcEbmDownConfigProperties;
import com.ericsson.enap.modules.ebmLog.thread.VgcEbmDownloadProcessThread;
import com.ericsson.enap.modules.sys.entity.EpcDict;
import com.ericsson.enap.modules.sys.service.EpcDictService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * VGCEBM本地文件下载处理
 */
@Component
public class VgcEnipEbmDownloadTask implements ManualTask {
    private Logger log = LoggerUtils.Logger(LogFileName.VgcEnipEbmDownLoadJob);
    @Autowired
    private EpcDictService epcDictService;
    @Autowired
    private VgcEbmDownConfigProperties vgcEbmDownConfigProperties;
    @Autowired
    private RedisCache redisCache;
    private static String ENIP="ENIP";

    @Override
    public void execute() {
        List<Map<String,String>>vgcServerEntities=vgcEbmDownConfigProperties.getListServer();
        List<Map<String,String>>vgcNetEntities=vgcEbmDownConfigProperties.getListNet();
        String storePath=downloadBasePath();
        log.info("=======EnipServer list:"+vgcServerEntities.size());
        CountDownLatch latch = new CountDownLatch(vgcNetEntities.size()*vgcServerEntities.size());
            for (Map<String,String> vgcServerEntity : vgcServerEntities) {
                for(Map<String,String> vgcNetEntity :vgcNetEntities) {
                    VgcNetEntity vgcEnipDownEntity=new VgcNetEntity();
                    vgcEnipDownEntity.setIp4(vgcServerEntity.get("ip"));
                    vgcEnipDownEntity.setId(Long.parseLong(vgcServerEntity.get("ip").replaceAll("\\.","")+vgcNetEntity.get("id")));
                    vgcEnipDownEntity.setName(ENIP+vgcEnipDownEntity.getId());
                    vgcEnipDownEntity.setUsername(vgcServerEntity.get("username"));
                    vgcEnipDownEntity.setPassword(vgcServerEntity.get("password"));
                    String netPath=File.separator +vgcNetEntity.get("id");
                    vgcEnipDownEntity.setDownloadPath(storePath + File.separator +vgcNetEntity.get("type")+ netPath);
                    VgcEbmDownloadProcessThread vgcEbmDownloadProcessThread = new VgcEbmDownloadProcessThread(vgcEnipDownEntity, storePath+ File.separator+ENIP+ netPath, ENIP, latch, log, redisCache);
                    new Thread(vgcEbmDownloadProcessThread).start();
                }
            }
        try {
            latch.await();
            log.info("===== All EnipServer download ebm files end! =====");
        } catch (InterruptedException e) {
            log.error("===== VgcEnipEbmDownloadTask execute error! ====="+e.getLocalizedMessage());
        }
    }

    /**
     * 获取下载文件存储的基础目录
     * @return
     */
    private String downloadBasePath(){
        EpcDict epcDict=new EpcDict();
        epcDict.setKeyName("EbmLogDownloadPath");
        epcDict= epcDictService.selectEpcDict(epcDict);
        if(null!=epcDict&&!"".equals(epcDict.getKeyValue())) {
            return epcDict.getKeyValue();
        }else{
            return "/opt";
        }
    }
}
